<?php

use Biz\cache\wiki\BrowseHistory as cBrowseHistory;
use Biz\model\wiki\Wiki as mWiki;
use Biz\service\wiki\Wiki as sWiki;
use Doraemon\service\statSign\Basic as basicStatSign;
use Doraemon\tools\ActionLog;
use Doraemon\tools\BlockType;

class InfoAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_NOTHING;

    public function main()
    {
        $uid = $this->uid;
        $wikiId = $this->getRequest()->getQuery('wiki_id', 0);
        $basicId = $this->getRequest()->getQuery('basic_id', 0);
        if (!$wikiId && $basicId) {
            $wikiInfo = mWiki::getByWhere(['basic_id' => $basicId, 'is_default' => 1, 'status[!]' => mWiki::STATUS_DEL]);
            $wikiId = $wikiInfo ? $wikiInfo['id'] : 0;
        }
        $info = sWiki::getDetail($wikiId, $uid);


        if ($info) {
            $rankingInfo = $info['ranking_info'] ?? null;
            if ($rankingInfo) {
                $rankingInfo["statSign"] = basicStatSign::genStatSign(basicStatSign::MODULE_WIKI_DETAIL_RANKING_LABEL_WIKI, [
                    basicStatSign::EXTEND_KEY_OBJ_ID => $info['basic_id'],
                    basicStatSign::EXTEND_KEY_OBJ_TYPE => BlockType::TYPE_GOODS_RANKING,
                    basicStatSign::EXTEND_KEY_RELATE_OBJ_ID => $wikiId,
                ]);
            }
            $info['ranking_info'] = $rankingInfo;
            $info['activity_info'] = $info['activity_info'] ? $info['activity_info'] : null;

            //2022住友津贴超级补贴日活动
            $day = date('Ymd');
            if ($day >= 20221010 && $day <= 20221030) {
                $superSubsidyDayIds = [268087, 268088, 268089, 268090, 253061, 173807, 265598, 254325, 100646, 238469, 259793, 231060, 100646, 266173, 131682, 265974, 265975, 189162, 11721, 265976, 259700, 265978, 260660, 265979, 222953, 239366, 249538, 257119, 238629, 265980, 255999, 227192, 265981, 265982, 264187, 266154, 192497, 115584, 172491, 257197, 263320, 264948, 259760, 266156, 206717, 196745, 194368, 115664, 102960, 266157, 253131, 196385, 159657, 152253, 152252, 266158, 257119, 249538, 241273, 238637, 238629, 233530, 266159, 115132, 120473, 266160, 257931, 95167, 87298, 266161, 266419, 259603, 259600, 259599, 259606, 266425, 259598];
                $delIds = [253061, 257197, 263320, 264948, 259760, 259600];
                $superSubsidyDayIds = array_diff($superSubsidyDayIds, $delIds);
                if (in_array($info['id'], $superSubsidyDayIds)) {
                    $link = \Doraemon\tools\Hhzurl::getDomain() . '/brand/sumitomo-allowance';
                    $info['float_banner'] = [
                        'id' => 0,
                        'title' => '',
                        'index' => 0,
                        'banner' => 'https://img.haohaozhu.cn/Op-imageShow/81577f05o05o08u00riyhzd?iv=1',
                        'link' => \Biz\tools\helper\Common::formatFullScreenLink($link),
                        'statSign' => basicStatSign::genStatSign(basicStatSign::MODULE_GOODS_BANNER, [
                            basicStatSign::EXTEND_KEY_POS => 'suspended_bottom_right',
                            basicStatSign::EXTEND_KEY_URL => $link,
                            basicStatSign::EXTEND_KEY_RELATE_OBJ_ID => $info['id'],
                            basicStatSign::EXTEND_KEY_RELATE_OBJ_TYPE => BlockType::TYPE_WIKI
                        ])
                    ];
                }
            }
        }

        //小鹅拼拼结构
        //广告活动已结束，不再展示
        /*if (in_array($info['channel'], [mWiki::CHANNEL_XIAOEPINPIN, mWiki::CHANNEL_AMAZON, mWiki::CHANNEL_XIAOMIYOUPIN])) {
            $channel = $info['channel'];
            $spuList = mWiki::selectByWhere([
                'channel_type' => $channel,
                'is_default' => mWiki::IS_DEFAULT,
                'status[!]' => [mWiki::STATUS_DEL, mWiki::STATUS_NORMAL],
                'LIMIT' => [0, 20]
            ], ['id', 'basic_id', 'cover_img']);
            $i = 0;
            foreach ($spuList as &$row) {
                $statSign = basicStatSign::genStatSign(basicStatSign::MODULE_WIKI_DETAIL_BRAND_CARD, [
                    basicStatSign::EXTEND_KEY_OBJ_ID => $row['basic_id'],
                    basicStatSign::EXTEND_KEY_OBJ_TYPE => BlockType::TYPE_WIKI_BASIC,
                    basicStatSign::EXTEND_KEY_INDEX => $i,
                ], ['wiki_id' => $info['basic_id']]);
                $row['statSign'] = $statSign;
                $i++;
            }
            $user_info = [];
            if ($channel == mWiki::CHANNEL_XIAOEPINPIN) {
                $user_info = [
                    'uid' => 0,
                    'nick' => '腾讯小鹅拼拼',
                    'avatar' => 'https://img.haohaozhu.cn/Op-imageShow/7c42c30sg0sg06n00qvvcoo?iv=1',
                ];
            }
            if ($channel == mWiki::CHANNEL_AMAZON) {
                $user_info = [
                    'uid' => 0,
                    'nick' => '亚马逊海外购',
                    'avatar' => 'https://img.haohaozhu.cn/Op-imageShow/1a16430go0go0Ik00qz1k5u?iv=1',
                ];
            }
            if ($channel == mWiki::CHANNEL_XIAOMIYOUPIN) {
                $user_info = [
                    'uid' => 0,
                    'nick' => '小米有品',
                    'avatar' => 'https://img.haohaozhu.cn/Op-imageShow/2f49a308i08l0Xf00qzgspa?iv=1',
                ];
            }
            $temp = [
                'brand_info' => [
                    'user_info' => $user_info,
                    'spu_list' => $spuList,
                ],
            ];
            $info = $temp + $info;
        }*/
        ActionLog::log($this->uid, ['wikiID' => $info ? $info['basic_id'] : 0, 'basic_id' => $info ? $info['basic_id'] : 0, 'wiki_id' => $info ? $info['id'] : 0], ActionLog::BIZ_WIKI_DETAIL_GOODSINFO);
        //追加浏览记录
        cBrowseHistory::addBrowseHistory($uid, $wikiId);
        $this->setSuccess($info);
    }
}